//
//  UISearchBar+TP.m
//  Tripvi
//
//  Created by xiaowuxiaowu on 2017/5/11.
//  Copyright © 2017年 xiaowuxiaowu. All rights reserved.
//

#import "UISearchBar+TP.h"
static const void *UISearchBarDelegateKey = &UISearchBarDelegateKey;
static const void *UISearchBarShouldBeginEditingKey = &UISearchBarShouldBeginEditingKey;
static const void *UISearchBarShouldEndEditingKey = &UISearchBarShouldEndEditingKey;
static const void *UISearchBarDidBeginEditingKey = &UISearchBarDidBeginEditingKey;
static const void *UIsearchBarSearchButtonClickedKey = &UIsearchBarSearchButtonClickedKey;
static const void *UISearchBarTextDidEndEditingKey = &UISearchBarTextDidEndEditingKey;

static const void *UISearchBarDidEndEditingKey = &UISearchBarDidEndEditingKey;
static const void *UISearchBarShouldChangeCharactersInRangeKey = &UISearchBarShouldChangeCharactersInRangeKey;
static const void *UISearchBarShouldClearKey = &UISearchBarShouldClearKey;
static const void *UISearchBarShouldReturnKey = &UISearchBarShouldReturnKey;

typedef BOOL (^UISearchBarReturnBlock) (UISearchBar *textField);
typedef void (^UISearchBarVoidBlock) (UISearchBar *textField);
typedef BOOL (^UISearchBarCharacterChangeBlock) (UISearchBar *textField, NSRange range, NSString *replacementString);
typedef void (^UISearchBarCharacterChangeTextBlock) (UISearchBar *textField, NSString *searchText);


@implementation  UISearchBar(TP)


+ (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
    UISearchBarReturnBlock block = searchBar.shouldBegindEditingBlock;
    if (block) {
        return block(searchBar);
    }
    id delegate = objc_getAssociatedObject(self, UISearchBarDelegateKey);
    if ([delegate respondsToSelector:@selector(searchBarShouldBeginEditing:)]) {
        return [delegate searchBarShouldBeginEditing:searchBar];
    }
    // return default value just in case
    return YES;
}

+ (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
    UISearchBarReturnBlock block = searchBar.shouldEndEditingBlock;
    if (block) {
        return block(searchBar);
    }
    id delegate = objc_getAssociatedObject(self, UISearchBarDelegateKey);
    if ([delegate respondsToSelector:@selector(searchBarShouldEndEditing:)]) {
        return [delegate searchBarShouldEndEditing:searchBar];
    }
    // return default value just in case
    return YES;
}
+ (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    UISearchBarVoidBlock block = searchBar.didBeginEditingBlock;
    if (block) {
        block(searchBar);
    }
    id delegate = objc_getAssociatedObject(self, UISearchBarDelegateKey);
    if ([delegate respondsToSelector:@selector(searchBarTextDidBeginEditing:)]) {
        [delegate searchBarTextDidBeginEditing:searchBar];
    }
}
+ (void)searchBarDidEndEditing:(UISearchBar *)searchBar
{
    UISearchBarVoidBlock block = searchBar.didEndEditingBlock;
    if (block) {
        block(searchBar);
    }
    id delegate = objc_getAssociatedObject(self, UISearchBarDelegateKey);
    if ([delegate respondsToSelector:@selector(searchBarDidEndEditing:)]) {
        [delegate searchBarTextDidEndEditing:searchBar];
    }
}
+ (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    UISearchBarCharacterChangeBlock block = searchBar.shouldChangeCharactersInRangeBlock;
    if (block) {
        return block(searchBar,range,string);
    }
    id delegate = objc_getAssociatedObject(self, UISearchBarDelegateKey);
    if ([delegate respondsToSelector:@selector(searchBar:shouldChangeCharactersInRange:replacementString:)]) {
        return [delegate searchBar:searchBar shouldChangeCharactersInRange:range replacementString:string];
    }
    return YES;
}
+ (BOOL)searchBarShouldClear:(UISearchBar *)searchBar
{
    UISearchBarReturnBlock block = searchBar.shouldClearBlock;
    if (block) {
        return block(searchBar);
    }
    id delegate = objc_getAssociatedObject(self, UISearchBarDelegateKey);
    if ([delegate respondsToSelector:@selector(searchBarShouldClear:)]) {
        return [delegate searchBarShouldClear:searchBar];
    }
    return YES;
}
+ (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    UISearchBarReturnBlock block = searchBar.shouldCannelBlock;
    if (block) {
        block(searchBar);
    }
    id delegate = objc_getAssociatedObject(self, UISearchBarDelegateKey);
    if ([delegate respondsToSelector:@selector(searchBarCancelButtonClicked:)]) {
         [delegate searchBarCancelButtonClicked:searchBar];
    }
    
}

+ (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    UISearchBarVoidBlock block = searchBar.buttonClickBlock;
    if (block) {
        block(searchBar);
    }
    id delegate = objc_getAssociatedObject(self, UIsearchBarSearchButtonClickedKey);
    if ([delegate respondsToSelector:@selector(searchBarSearchButtonClicked:)]) {
        [delegate searchBarSearchButtonClicked:searchBar];
    }
}

+(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    UISearchBarCharacterChangeTextBlock block = searchBar.textDidChangeBlock;
    if (block) {
        block(searchBar,searchText);
    }
    id delegate = objc_getAssociatedObject(self, UISearchBarTextDidEndEditingKey);
    if ([delegate respondsToSelector:@selector(searchBar:textDidChange:)]) {
        [delegate searchBar:searchBar textDidChange:searchText];
    }
}

#pragma mark Block setting/getting methods
- (BOOL (^)(UISearchBar *))shouldBegindEditingBlock
{
    return objc_getAssociatedObject(self, UISearchBarShouldBeginEditingKey);
}
- (void)setShouldBegindEditingBlock:(BOOL (^)(UISearchBar *))shouldBegindEditingBlock
{
    [self setDelegateIfNoDelegateSet];
    objc_setAssociatedObject(self, UISearchBarShouldBeginEditingKey, shouldBegindEditingBlock, OBJC_ASSOCIATION_COPY);
}
- (BOOL (^)(UISearchBar *))shouldEndEditingBlock
{
    return objc_getAssociatedObject(self, UISearchBarShouldEndEditingKey);
}
- (void)setShouldEndEditingBlock:(BOOL (^)(UISearchBar *))shouldEndEditingBlock
{
    [self setDelegateIfNoDelegateSet];
    objc_setAssociatedObject(self, UISearchBarShouldEndEditingKey, shouldEndEditingBlock, OBJC_ASSOCIATION_COPY);
}
- (void (^)(UISearchBar *))didBeginEditingBlock
{
    return objc_getAssociatedObject(self, UISearchBarDidBeginEditingKey);
}
- (void)setDidBeginEditingBlock:(void (^)(UISearchBar *))didBeginEditingBlock
{
    [self setDelegateIfNoDelegateSet];
    objc_setAssociatedObject(self, UISearchBarDidBeginEditingKey, didBeginEditingBlock, OBJC_ASSOCIATION_COPY);
}
- (void (^)(UISearchBar *))didEndEditingBlock
{
    return objc_getAssociatedObject(self, UISearchBarDidEndEditingKey);
}
- (void)setDidEndEditingBlock:(void (^)(UISearchBar *))didEndEditingBlock
{
    [self setDelegateIfNoDelegateSet];
    objc_setAssociatedObject(self, UISearchBarDidEndEditingKey, didEndEditingBlock, OBJC_ASSOCIATION_COPY);
}
- (BOOL (^)(UISearchBar *, NSRange, NSString *))shouldChangeCharactersInRangeBlock
{
    return objc_getAssociatedObject(self, UISearchBarShouldChangeCharactersInRangeKey);
}
- (void)setShouldChangeCharactersInRangeBlock:(BOOL (^)(UISearchBar *, NSRange, NSString *))shouldChangeCharactersInRangeBlock
{
    [self setDelegateIfNoDelegateSet];
    objc_setAssociatedObject(self, UISearchBarShouldChangeCharactersInRangeKey, shouldChangeCharactersInRangeBlock, OBJC_ASSOCIATION_COPY);
}
- (BOOL (^)(UISearchBar *))shouldCannelBlock
{
    return objc_getAssociatedObject(self, UISearchBarShouldReturnKey);
}
- (void)setShouldCannelBlock:(BOOL (^)(UISearchBar *))shouldReturnBlock
{
    [self setDelegateIfNoDelegateSet];
    objc_setAssociatedObject(self, UISearchBarShouldReturnKey, shouldReturnBlock, OBJC_ASSOCIATION_COPY);
}
- (BOOL (^)(UISearchBar *))shouldClearBlock
{
    return objc_getAssociatedObject(self, UISearchBarShouldClearKey);
}
- (void)setShouldClearBlock:(BOOL (^)(UISearchBar *searchBar))shouldClearBlock
{
    [self setDelegateIfNoDelegateSet];
    objc_setAssociatedObject(self, UISearchBarShouldClearKey, shouldClearBlock, OBJC_ASSOCIATION_COPY);
}


- (void (^)(UISearchBar *))buttonClickBlock
{
    return objc_getAssociatedObject(self, UIsearchBarSearchButtonClickedKey);
}
- (void)setButtonClickBlock:(void (^)(UISearchBar *))clickedBlock
{
    [self setDelegateIfNoDelegateSet];
    objc_setAssociatedObject(self, UIsearchBarSearchButtonClickedKey, clickedBlock, OBJC_ASSOCIATION_COPY);
}
- (void (^)(UISearchBar *, NSString *))textDidChangeBlock
{
    return objc_getAssociatedObject(self, UISearchBarTextDidEndEditingKey);
}
-(void)setTextDidChangeBlock:(void (^)(UISearchBar *, NSString *))barTextDidEndEditingBlock{
    [self setDelegateIfNoDelegateSet];
    objc_setAssociatedObject(self, UISearchBarTextDidEndEditingKey, barTextDidEndEditingBlock, OBJC_ASSOCIATION_COPY);
}
#pragma mark control method
/*
 Setting itself as delegate if no other delegate has been set. This ensures the UISearchBar will use blocks if no delegate is set.
 */


- (void)setDelegateIfNoDelegateSet
{
    if (self.delegate != (id<UISearchBarDelegate>)[self class]) {
        objc_setAssociatedObject(self, UISearchBarDelegateKey, self.delegate, OBJC_ASSOCIATION_ASSIGN);
        self.delegate = (id<UISearchBarDelegate>)[self class];
    }
}

-(void)tp_setupSearchBarPlaceholder:(NSString *)placeholder{
    
//    [self setBackgroundImage:[UIImage imageWithColor:TP_COLOR_245] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsCompactPrompt];
//    [self setSearchFieldBackgroundImage:[UIImage imageWithColor:[UIColor redColor]] forState:UIControlStateNormal];
    [self setScopeBarButtonBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [self setImage:ImageNamed(@"search_icon") forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
    [self setImage:ImageNamed(@"cannel_black") forSearchBarIcon:UISearchBarIconClear state:UIControlStateNormal];
//    self.barStyle = UIBarStyleBlackTranslucent;
    self.searchBarStyle = UISearchBarStyleMinimal;
    self.placeholder = placeholder;
   
    self.returnKeyType = UIReturnKeySearch;
    self.translucent = YES;
//    [self tp_addLineBottomWithColor:TPBlack_999999];
    [self setShouldBegindEditingBlock:^BOOL(UISearchBar *earchBar) {
        [earchBar setShowsCancelButton:YES animated:YES];
       
        return YES;
    }];
    
    [self setDidBeginEditingBlock:^(UISearchBar *earchBar) {
        NSArray *subViews;
        
        if (IOS7) {
            subViews = [(earchBar.subviews[0]) subviews];
        }
        else {
            subViews = earchBar.subviews;
        }
        
        for (id view in subViews) {
            if ([view isKindOfClass:[UIButton class]]) {
                UIButton* cancelbutton = (UIButton* )view;
                [cancelbutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
               
                // 修改文字颜色
                
                break;
            }
        }

    }];
    
    [self setShouldEndEditingBlock:^BOOL(UISearchBar *earchBar) {
        [earchBar setShowsCancelButton:NO animated:YES];
        return YES;
    }];
    
    [self setShouldCannelBlock:^BOOL(UISearchBar *earchBar) {
        [earchBar resignFirstResponder];
        [earchBar setShowsCancelButton:NO animated:YES];
        return YES;
    }];
  
    
    
}


@end
